<?php
namespace app\admin\controller;
class File
{
	public function __construct(){
		Admin_auth();
		$config = db('material_config')->where('id',1)->find();
		config([
			'UPLOAD_SITEIMG_QINIU'=>[
			'maxSize'          => 5 * 1024 * 1024,//文件大小
			'rootPath'         => './',
			'saveName'         => ['uniqid', ''],
			'driver'           => 'Qiniu',
				'driverConfig' => [
					'secretKey' => $config['secretKey'], 
					'accessKey' => $config['accessKey'],
					'domain'    => $config['domain'],
					'bucket'    => $config['bucket'],
				],
			],
			'file_size'=>$config['file_size']
		]);
	}
	
    public function index()
    {
		$type = input("get.type")?input("get.type"):'image';
		$pages = pages('material',['type'=>$type],input('get.page'));
		$data = db("material")->where('type',$type)->limit($pages['limit'])->order('id desc')->select();
		$size = db('material')->sum('size');
        return view('',['data'  => $data,'pages' => $pages['pages'],'size' => $size,'type' => $type]);
    }
	
	public function del()
    {
		$id = input('get.id');
		$type = input("get.type")?input("get.type"):'image';
		$page = input('get.page');
		$material = db('material');
		$key = $material->whereIn('id',$id)->column('name');
		if(empty($key)){
			return error();
			exit;
		}
		$setting=config('UPLOAD_SITEIMG_QINIU');
		$Upload = new \think\Upload($setting);
		$info = $Upload->delBatch($key);
		
		if($info != 'false'){
			if($material->delete($id)>0){
				redirect(url('/admin/file').'?type='.$type.'&page='.$page);
				exit;
			}
		}
		
    }
	
	public function setting()
    {
		if(request()->isPost()){
			$data = input("post.");
			db("material_config")->where(['id'=>1])->update($data);
		}
		$data = db("material_config")->where(['id'=>1])->find();
        return view('',['data' => $data]);
    }
}
